Path : Path tools
The path
module provides basic path and file name analysis. This module is compatible with Node.js.
User can use the following code to import the path
module.
var path = require('path');
Support
The following shows path
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
path.basename | ● | ● |
path.delimiter | ● | ● |
path.dirname | ● | ● |
path.extname | ● | ● |
path.format | ● | ● |
path.isAbsolute | ● | ● |
path.join | ● | ● |
path.normalize | ● | ● |
path.parse | ● | ● |
path.resolve | ● | ● |
path.sep | ● | ● |
path.cwd | ● | ● |
path.toNamespacedPath | ● | ● |
Path Object
path.basename(path[, ext])
path
{String} Path.ext
{String} An optional file extension. default: ''.- Returns: {String} Result.
The path.basename()
methods returns the last portion of a path
, similar to the Unix basename
command. Trailing directory separators are ignored, see path.sep
.
Example
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
path.delimiter
- {String}
Provides the platform-specific path delimiter, In SylixOS it must be: ':'
.
Example
var binSearchPath = '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin';
binSearchPath.split(path.delimiter);
// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
path.dirname(path)
path
{String} Path.- Returns: {String} Result.
The path.dirname()
method returns the directory name of a path
, similar to the Unix dirname
command. Trailing directory separators are ignored, see path.sep
.
Example
path.dirname('/foo/bar/baz/asdf/quux');
// Returns: '/foo/bar/baz/asdf'
path.extname(path)
path
{String} Path.- Returns: {String} Result.
The path.extname()
method returns the extension of the path
, from the last occurrence of the .
(period) character to end of string in the last portion of the path
. If there is no .
in the last portion of the path
, or if the first character of the basename of path
(see path.basename()
) is .
, then an empty string is returned.
Example
path.extname('index.html');
// Returns: '.html'
path.extname('index.coffee.md');
// Returns: '.md'
path.extname('index.');
// Returns: '.'
path.extname('index');
// Returns: ''
path.extname('.index');
// Returns: ''
path.format(pathObject)
pathObject
{Object} Path object.- Returns: {String} Result.
pathObject
includes following items:
dir
{String} Directory.root
{String} Root.base
{String} Base name.name
{String} Name.ext
{String} Extension.
The path.format()
method returns a path string from an object. This is the opposite of path.parse()
.
When providing properties to the pathObject
remember that there are combinations where one property has priority over another:
pathObject.root
is ignored ifpathObject.dir
is provided.pathObject.ext
andpathObject.name
are ignored ifpathObject.base
exists.
Example
// If `dir`, `root` and `base` are provided,
// `${dir}${path.sep}${base}`
// will be returned. `root` is ignored.
path.format({
root: '/ignored',
dir: '/home/user/dir',
base: 'file.txt'
});
// Returns: '/home/user/dir/file.txt'
// `root` will be used if `dir` is not specified.
// If only `root` is provided or `dir` is equal to `root` then the
// platform separator will not be included. `ext` will be ignored.
path.format({
root: '/',
base: 'file.txt',
ext: 'ignored'
});
// Returns: '/file.txt'
// `name` + `ext` will be used if `base` is not specified.
path.format({
root: '/',
name: 'file',
ext: '.txt'
});
// Returns: '/file.txt'
path.isAbsolute(path)
path
{String} Path.- Returns: {Boolean} Is this path is a absolute path.
The path.isAbsolute()
method determines if path
is an absolute path. If the given path
is a zero-length string, false will be returned.
Example
path.isAbsolute('/foo/bar'); // true
path.isAbsolute('/baz/..'); // true
path.isAbsolute('qux/'); // false
path.isAbsolute('.'); // false
path.join([...paths])
...paths
{String} A sequence of path segments.- Returns: {String} Result.
The path.join()
method joins all given path
segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
Zero-length path
segments are ignored. If the joined path string is a zero-length string then '.'
will be returned, representing the current working directory.
Example
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// Returns: '/foo/bar/baz/asdf'
path.join('foo', {}, 'bar');
// throws 'TypeError: Path must be a string. Received {}'
path.normalize(path)
path
{String} Path.- Returns: {String} Result.
The path.normalize()
method normalizes the given path
, resolving '..'
and '.'
segments.
When multiple, sequential path segment separation characters are found (/
), they are replaced by a single instance of the platform-specific path segment separator (/
). Trailing separators are preserved.
If the path
is a zero-length string, '.'
is returned, representing the current working directory.
Example
path.normalize('/foo/bar//baz/asdf/quux/..');
// Returns: '/foo/bar/baz/asdf'
path.parse(path)
path
{String} Path.- Returns: {Object} Result.
The path.parse()
method returns an object whose properties represent significant elements of the path
. Trailing directory separators are ignored, see path.sep
.
The returned object will have the following properties:
dir
{String} Directory.root
{String} Root.base
{String} Base name.name
{String} Name.ext
{String} Extension.
Example
path.parse('/home/user/dir/file.txt');
// Returns:
// { root: '/',
// dir: '/home/user/dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file' }
path.resolve([...paths])
...paths
{String} A sequence of paths or path segments.- Returns: {String} Result.
The path.resolve()
method resolves a sequence of paths or path segments into an absolute path.
The given sequence of paths is processed from right to left, with each subsequent path
prepended until an absolute path is constructed. For instance, given the sequence of path segments: /foo
, /bar
, baz
, calling path.resolve('/foo', '/bar', 'baz')
would return /bar/baz
.
If after processing all given path
segments an absolute path has not yet been generated, the '/'
is used.
Zero-length path segments are ignored.
Example
path.resolve('/foo/bar', './baz');
// Returns: '/foo/bar/baz'
path.resolve('/foo/bar', '/tmp/file/');
// Returns: '/tmp/file'
path.sep
- {String}
Provides the platform-specific path segment separator:
'/'
on SylixOS.
Example
'foo/bar/baz'.split(path.sep);
// Returns: ['foo', 'bar', 'baz']
path.cwd()
- Returns: {String} Current working directory.
Get the current working directory. Since JSRE App is SandBox management, User Mode App calls this method the return value is always '/'
.
path.toNamespacedPath(path)
path
{String} Path.- Returns: {String} Result.
Always returns path
without modifications.